#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

void usage ( char *name )
{
   fprintf ( stderr,"Usage:\n%s file directory\n", name );
   fprintf ( stderr,"List files that are more recently modified than the given file.\n" );
   exit ( 1 );
}

void exitError ( char *file,
                 char *action )
{
   perror ( "Exiting" );
   fprintf ( stderr,"Error while %s on %s.\n",action,file );
   exit ( 3 );
}

int isDirectory ( char *path )
{
   struct stat statbuf;
   if ( stat ( path, &statbuf ) == -1 )
   {
      exitError ( path, "accessing" );
   }
   else
   {
      return ( S_ISDIR ( statbuf.st_mode ));
   }
}

time_t lastModified ( char *path )
{
   struct stat statbuf;
   if ( stat ( path, &statbuf ) == -1 )
   {
      exitError ( path, "accessing" );
   }
   return ( statbuf.st_mtime );
}

int compareTime ( char *file1, char *file2 )
{
   if ( lastModified ( file1 ) < lastModified ( file2 ))
   {
      return 1;
   }
   return 0;
}

int main(int argc, char** argv)
{
   if ( argc != 3 )
   {
      usage ( argv[0] );
   }

<<<<<<< .mine
	if(!isDirectory(argv[2])) {
		if(compareTime(argv[1], argv[2])) {
			printf("%s\n", argv[2]);
			exit(0);
		}
		else exit(2);
	}
	else if(isDirectory(argv[2])) {
		struct dirent *direntp;
		DIR *dirp;
		if((dirp = opendir(argv[2])) == NULL) {
			//fprintf(stderr, "Error. cannot open directory\n");
			exitError(argv[2], "opening");
		}
		int flag = 0;
		while((direntp = readdir(dirp))!= NULL) {
			if(compareTime( (argv[1]), (direntp->d_name))) {
				printf("%s\n", direntp->d_name);
				flag = 1;
			}
		}
		while((closedir(dirp) == -1) && (errno == EINTR));
		if(flag) {
			exit(0);
		}
		else exit(2);		
	}
	exit(3);
=======
   if ( isDirectory ( argv[1] ))
   {
      usage ( argv[0] );
   }

   if ( !isDirectory ( argv[2] ))
   {
      if ( compareTime ( argv[1], argv[2] ))
      {
         printf ( "%s\n", argv[2] );
         exit ( 0 );
      }
      else
      {
         exit(2);
      }
   }
   else if ( isDirectory ( argv[2] ))
   {
      struct dirent *direntp;
      DIR *dirp;
      if (( dirp = opendir ( argv[2] )) == NULL )
      {
         //fprintf ( stderr, "Error. cannot open directory\n" );
         exitError ( argv[2], "opening" );
      }
      int flag = 0;
      while (( direntp = readdir ( dirp )) != NULL )
      {
         if ( compareTime (( argv[1] ), ( direntp->d_name )))
         {
            printf ( "%s\n", direntp->d_name );
            flag = 1;
         }
      }
      while (( closedir ( dirp ) == -1 ) && ( errno == EINTR ));
      if ( flag )
      {
         exit ( 0 );
      }
      else
      {
         exit ( 2 );
      }
   }
   exit ( 3 );
>>>>>>> .r25
}
